package com.pateo.bjev.contacts.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pateo.bjev.contacts.BjevContactsManager;
import com.pateo.bjev.contacts.BtData;
import com.pateo.bjev.contacts.MainActivity;
import com.pateo.bjev.contacts.R;
import com.pateo.bjev.contacts.adapter.CallItemDecoration;
import com.pateo.bjev.contacts.adapter.ContactAdapter;
import com.pateo.bjev.contacts.view.ContactSideBar;
import com.pateo.bluetooth.logic.bean.ContactData;

import java.util.List;

/**
 * Created by pateo on 17-5-9.
 */

public class ContactsFragment extends Fragment {

    View contentView;

    RecyclerView contact_list;
    ContactSideBar sideBar;

    BjevContactsManager manager;
    View no_connect_view;

    ContactAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.contacts_layout,null);
        init();
        initState();
        return contentView;
    }

    private void init(){
        contact_list = (RecyclerView) contentView.findViewById(R.id.contact_list);
        sideBar = (ContactSideBar) contentView.findViewById(R.id.contact_side);
        no_connect_view = contentView.findViewById(R.id.no_connect);
        contentView.findViewById(R.id.btn_connect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity main = (MainActivity) getActivity();
                main.toBtSetting();
            }
        });
        adapter = new ContactAdapter(getContext(),null);
        contact_list.setLayoutManager(new LinearLayoutManager(getContext()));
        contact_list.addItemDecoration(new CallItemDecoration(getContext(),LinearLayoutManager.HORIZONTAL,1, Color.WHITE));
        contact_list.setAdapter(adapter);
        sideBar.setListView(contact_list);
    }

    private void initState(){
        manager = ((MainActivity)getContext()).getManager();
        if(manager!=null){
            try {
                switch (manager.getBtState()){
                    case BtData.BT_STATE_CONNECTED:
                        switchContacts(true);
                        break;
                    case BtData.BT_STATE_DISCONNECTED:
                        switchContacts(false);
                        break;
                    default:
                        break;
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void switchContacts(boolean visible){
        if(!isAdded()){
            return;
        }
        Transition transition = TransitionInflater.from(getContext()).inflateTransition(R.transition.data_loaded);
        TransitionManager.beginDelayedTransition((ViewGroup)contentView,transition);
        if(visible){
            logContacts();
            contact_list.setVisibility(View.VISIBLE);
            no_connect_view.setVisibility(View.GONE);
        }else {
            contact_list.setVisibility(View.GONE);
            no_connect_view.setVisibility(View.VISIBLE);
        }
    }

    private void logContacts(){
        BjevContactsManager manager = ((MainActivity)getContext()).getManager();
        try {
            List<ContactData> contacts = manager.getContacts();
            adapter.updateContactList(contacts);
            sideBar.updateLetters();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void logContacts(List<ContactData> list){
        if(list==null){
            return;
        }
        adapter.updateContactList(list);
        sideBar.updateLetters();
    }
}
